package hk;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Test {
    public static void main(String[] args) {
        String filePath = "D:\\code\\gitee\\java\\src\\main\\resources\\file.txt";

        try {
            // 从文件中读取所有行
            List<String> lines = readLinesFromFile(filePath);

            // 按行排序
            sortLines(lines);

            // 打印排序后的文本
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> readLinesFromFile(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }

    public static void sortLines(List<String> lines) {
        // 创建Collator对象，并设置比较规则为按拼音排序
        Collator collator = Collator.getInstance(Locale.CHINA);
        collator.setStrength(Collator.PRIMARY);
        lines.sort(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return collator.compare(s1, s2);
            }
        });
    }
}

